package stock;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TestYinShou {

	public static void main(String[] args) {

		String[] str = TestYinShou.getYJ("");
		
		System.out.println(str[0]);
		System.out.println(str[1]);
	}

	public static String[] getYJ(String code) {
		String[] all = new String[] { "", "", "" };
		try {

			String url = "https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code="+code+"&color=b#/cwfx";
			if (driver == null) {
				driver = getdriver();
			}
			driver.get(url);

			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
			// 抓取网页内容
			// String pageContent = driver.getPageSource();
			List<WebElement> elements0 = driver.findElements(By.xpath("//table[@class='table1']//tr[10]//td[2]"));

			for (Iterator iterator = elements0.iterator(); iterator.hasNext();) {
				WebElement webElement = (WebElement) iterator.next();

				//System.out.println(webElement.getText());

				all[0] = webElement.getText();

			}

			List<WebElement> elements = driver.findElements(By.xpath("//table[@class='table1']//tr[12]//td[2]"));

			for (Iterator iterator = elements.iterator(); iterator.hasNext();) {
				WebElement webElement = (WebElement) iterator.next();

				//System.out.println(webElement.getText());
				all[1] = webElement.getText();
			}

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("调用发生错误");
		}
		return all;
	}

	public static WebDriver driver = null;

	public static WebDriver getdriver() {

		if (driver == null) {
			File f = new File("D:\\chromedriver\\chromedriver-win64124");
			if (f.exists()) {
				System.setProperty("webdriver.chrome.driver",
						"D:\\chromedriver\\chromedriver-win64127\\chromedriver-win64\\chromedriver.exe");
			} else {
				System.setProperty("webdriver.chrome.driver", "/opt/chromedriver-linux64/chromedriver");
			}
			// 指定Chrome浏览器核心

			// 创建Chrome浏览器驱动
			// 配置Chrome选项
			ChromeOptions options = new ChromeOptions();
			options.addArguments("--headless"); // 无界面运行
			options.addArguments("--no-sandbox");
			options.addArguments("--disable-gpu");
			options.addArguments("--disable-dev-shm-usage");
			// 创建Chrome浏览器驱动
			driver = new ChromeDriver(options);
		}
		return driver;
	}
}
